﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsGame1
{

    public static class IEnumerableExtensions
    {

        /// <summary>
        /// http://nickstips.wordpress.com/2010/08/28/c-optimized-extension-method-get-a-random-element-from-a-collection/
        /// </summary>
        public static T ElementAt<T>(this IEnumerable<T> source, int index)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            T current;
            IList<T> list = source as IList<T>;
            if (list != null)
                return list[index];
            return source.Skip(index).First();
        }
        public static T GetRandom<T>(this IEnumerable<T> source, Random rnd)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            var count = source.Count();
            if (count < 1)
                throw new ArgumentOutOfRangeException("source");
            var index = rnd.Next(0, count - 1);
            return source.ElementAt(index);
        }
    }
}
